package cn.lili.controller.feign;

import cn.hutool.core.collection.CollectionUtil;
import cn.lili.common.exception.ServiceException;
import cn.lili.modules.promotion.client.BoxLotteryTicketClient;
import cn.lili.modules.promotion.client.BoxMhGoodsClient;
import cn.lili.modules.promotion.entity.dos.BoxLotteryTicket;
import cn.lili.modules.promotion.entity.dto.CallbackBlindboxDTO;
import cn.lili.modules.promotion.service.BoxLotteryTicketService;
import cn.lili.modules.promotion.service.BoxMhgoodsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor
public class BoxLotteryTicketFeignController implements BoxLotteryTicketClient {

    private final BoxLotteryTicketService boxLotteryTicketService;

    /**
     * 回收奖券
     * @param id 订单ID
     * @param remark 备注
     */
    @Override
    public boolean recycle(String id, String remark) {
        log.info("回收奖券 == 订单id {} , 回收备注 === {}", id , remark);
        LambdaQueryWrapper<BoxLotteryTicket> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BoxLotteryTicket::getOrderId, id);
        List<BoxLotteryTicket> ticketList = boxLotteryTicketService.list(lambdaQueryWrapper);
        if (CollectionUtil.isEmpty(ticketList)) {
            log.info("未获取到奖券信息。订单id == {}", id);
            throw new ServiceException("未获取到奖券信息");
        }
        for (BoxLotteryTicket boxLotteryTicket : ticketList) {
            boxLotteryTicketService.cancellation(boxLotteryTicket.getId(), remark);
        }
        return true;
    }
}
